package cn.test.dao.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collection;

import cn.test.Exception.ExistingException;
import cn.test.Exception.NoExistingException;
import cn.test.model.Account;

public class AccountFileImpl implements AccountFile {
	private ArrayList<Account> accountList = new ArrayList<Account>();

	@Override
	public Collection<Account> getAllAccount() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean addAccount(Account account) throws ExistingException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAccount(String account) throws NoExistingException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean alterAccount(String account, String password) throws NoExistingException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Account lookAccount(String account) throws NoExistingException {
		// TODO Auto-generated method stub
		return null;
	}
	 public void readArray() {
		 File file = new File("C:\\Users\\hp\\git\\Test\\src\\cn\\test\\common\\userInfo");
		 String str = null;
		 BufferedReader br = null;
		 FileReader fr = null;
		 
		 try {
			 fr = new FileReader(file);
			 br = new BufferedReader(fr);
			 
			 while((str=br.readLine())!= null) {
				 String[]  in = str.split("   ");
				 Account ac = new Account(in[0],in[1],in[2]);
				 accountList.add(ac);
			 }
			 for(Account a1:accountList) {
				 System.out.println(a1);
			 }
		 }catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				br.close();
				fr.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	 }
	 public void writeFile() {
		 File file = new File("C:\\Users\\hp\\git\\Test\\src\\cn\\test\\common\\userInfo.txt");
		 FileWriter fw = null;
		 BufferedWriter bw = null;
		 accountList.add(new Account("1","1","1"));
		 try {
			 fw = new FileWriter(file);
			 bw = new BufferedWriter(fw);
			 for(Account ac:accountList) {
				 bw.write(ac.getAccount()+" "+ac.getName()+" "+ac.getMoney());
			 }
		 }catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				bw.close();
				fw.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	 }
	 public static void main(String[] args) {
		AccountFileImpl afi = new AccountFileImpl();
		afi.writeFile();
	}

}
